const router = require('express').Router({});
const {check, validationResult} = require('express-validator'); //数据验证
const courseControllers = require("../../controllers/courseControllers");
/* GET */
router.get(
    '/',
    [
        check('pageNo').notEmpty().withMessage('页码不能为空'),
        check('pageSize').notEmpty().withMessage('条数不能为空')
    ],
    async function (req, res, next) {
        const errors = validationResult(req)
        if (!errors.isEmpty()) {
            return res.sendResult(null, 400, errors.array()[0].msg);
        }
        let data = await courseControllers
            .selectList(
                req.query.pageNo,
                req.query.pageSize,
                req.query.cname,
                req.query.tname,
                req.query.department
            );

        res.sendResult(data, 200, "请求成功");

    });

let dateTime = /([1-2][0-9][0-9][0-9]-[0-1]?[0-9]-[0-3]?[0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9])|([1-2][0-9][0-9][0-9]-[0-1]{0,1}[0-9]-[0-3]{0,1}[0-9])/;

router.post(
    '/',
    [
        check('course_teacher_id').notEmpty().withMessage('course_teacher_id参数不能为空'),
        check('course_name').notEmpty().withMessage('course_name参数不能为空'),
        check('course_grade').notEmpty().withMessage('course_grade参数不能为空')
            .custom(course_grade => {
                if (!(/\d{4}/.test(course_grade))) {
                    throw new Error('course_grade因为4位年份')
                }
                return true
            }),
        check('course_time').notEmpty().withMessage('course_time参数不能为空')
            .custom(course_time => {
                if (!(/[1-5]-[0-9]-[1-4]/.test(course_time))) {
                    throw new Error('course_time应满足/[1-5]-[0-9]-[1-4]/')
                }
                return true
            }),
        check('course_location').notEmpty().withMessage('course_location参数不能为空'),
        check('course_credit').notEmpty().withMessage('course_credit参数不能为空')
            .custom(course_credit => {
                if (!(/\d/.test(course_credit))) {
                    throw new Error('course_credit因为数值')
                }
                return true
            }),
        check('course_max_size').notEmpty().withMessage('course_max_size参数不能为空')
            .custom(course_max_size => {
                if (!(/\d+/.test(course_max_size))) {
                    throw new Error('course_max_size因为数值')
                }
                return true
            }),
        check('course_exam_date').notEmpty().withMessage('course_exam_date参数不能为空')
            .custom(course_exam_date => {
                if (!dateTime.test(course_exam_date)) {
                    throw new Error('course_exam_date不是合法日期 yyyy-MM-dd hh:mm:dd')
                }
                return true
            }),
        check('course_exam_location').notEmpty().withMessage('course_exam_location参数不能为空')
    ],
    async function (req, res, next) {
        const errors = validationResult(req)
        if (!errors.isEmpty()) {
            return res.sendResult(null, 400, errors.array()[0].msg);
        }

        await courseControllers.insert(req.body)

        res.sendResult({}, 201, "添加成功");
    }
);

router.put(
    '/',
    [
        check('course_id').notEmpty().withMessage('course_id参数不能为空'),
        check('course_teacher_id').notEmpty().withMessage('course_teacher_id参数不能为空'),
        check('course_name').notEmpty().withMessage('course_name参数不能为空'),
        check('course_grade').notEmpty().withMessage('course_grade参数不能为空')
            .custom(course_grade => {
                if (!(/\d{4}/.test(course_grade))) {
                    throw new Error('course_grade因为4位年份')
                }
                return true
            }),
        check('course_time').notEmpty().withMessage('course_time参数不能为空')
            .custom(course_time => {
                if (!(/[1-5]-[0-9]-[1-4]/.test(course_time))) {
                    throw new Error('course_time应满足/[1-5]-[0-9]-[1-4]/')
                }
                return true
            }),
        check('course_location').notEmpty().withMessage('course_location参数不能为空'),
        check('course_credit').notEmpty().withMessage('course_credit参数不能为空')
            .custom(course_credit => {
                if (!(/\d/.test(course_credit))) {
                    throw new Error('course_credit因为数值')
                }
                return true
            }),
        check('course_max_size').notEmpty().withMessage('course_max_size参数不能为空')
            .custom(course_max_size => {
                if (!(/\d+/.test(course_max_size))) {
                    throw new Error('course_max_size因为数值')
                }
                return true
            }),
        check('course_exam_date').notEmpty().withMessage('course_exam_date参数不能为空')
            .custom(course_exam_date => {
                if (!dateTime.test(course_exam_date)) {
                    throw new Error('course_exam_date不是合法日期 yyyy-MM-dd hh:mm:dd')
                }
                return true
            }),
        check('course_exam_location').notEmpty().withMessage('course_exam_location参数不能为空')
    ],
    async function (req, res, next) {
        const errors = validationResult(req)
        if (!errors.isEmpty()) {
            return res.sendResult(null, 400, errors.array()[0].msg);
        }

        let data1 = await courseControllers.selectById(req.body.course_id);
        if (!data1) {
            res.sendResult(null, 400, "更新失败,course_id不存在");
            return;
        }

        let flag = await courseControllers.updateByDid(req.body);
        res.sendResult({}, 202, flag ? "更新成功" : "数据已更新");
    }
);

router.delete(
    '/',
    [
        check('course_id').notEmpty().withMessage('course_id参数不能为空'),
    ],
    async function (req, res, next) {
        const errors = validationResult(req)
        if (!errors.isEmpty()) {
            return res.sendResult(null, 400, errors.array()[0].msg);
        }

        let data = await courseControllers.selectById(req.query.course_id);
        if (!data) {
            res.sendResult(null, 400, "删除失败,course_id不存在");
            return;
        }

        let flag = await courseControllers.deleteByDid(req.query.course_id);
        res.sendResult({}, 203, flag ? "删除成功" : "数据已删除");
    }
);

module.exports = router;
  
